package smart
{
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedSuperclassName;
	
	import smart.command.Command;
	import smart.command.SingletenCommand;
	import smart.utils.SingletonClassFactory;
	/**
	 *  
	 * @author Eric li
	 * 
	 */	
	public class CommandMap extends  CoreConnector
	{
		
		protected static var dicMap:Dictionary;
		public function mapEvent(type:String,command:Object,eventClass:Class):void
		{
			if(dicMap[type]==null)
			{
				dicMap[type]=new Array();
			}
			(dicMap[type] as Array).push(command);
			this.eventDispatcher.addEventListener(type,this.eventHander);
		}
		
		
		public function CommandMap(target:IEventDispatcher=null)
		{
			super(target);
			if(dicMap==null)
			{
				dicMap=new Dictionary();
			}
			
		}
		public function eventHander(e:Event):void
		{
			var type:String=e.type;
			var array:Array=dicMap[type];
			
			for each (var commandClass:Class in array) 
			{
				if(commandClass==null)
				{
					continue;
				}
				var cmd:Object;
				var commandClassName:String=getQualifiedSuperclassName(commandClass);
				var superClass:Class=getDefinitionByName(commandClassName) as Class;
				if(superClass == SingletenCommand)
				{ 	
					
					cmd=SingletonClassFactory.getSingletonInstance(commandClass);
				}else{
					cmd=new commandClass();
				}
				
				cmd.event=e;
				if(cmd is Command)
				{
					cmd.execute();
				}
			}
			
			
			
			
		}
	}
}